package com.talent.jiwen.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.talent.jiwen.R;
import com.talent.jiwen.http.common.ApiFactory;
import com.talent.jiwen.http.common.HttpUtil;
import com.talent.jiwen.http.common.ProgressSubscriber;
import com.talent.jiwen.http.result.Member;
import com.talent.jiwen.http.result.OrderResult;
import com.talent.jiwen.teacher.OrderedTeacherListActivity;
import com.talent.jiwen.ui.widgets.CourseInfoView;
import com.talent.jiwen.util.MyToast;
import com.talent.jiwen.util.SPConstant;
import com.talent.jiwen.util.SPUtil;
import com.talent.jiwen.util.Validators;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Observable;

public class CourseListAdapter extends RecyclerView.Adapter<CourseListAdapter.CourseListViewHolder>{

    private Context context;
    private List<OrderResult.Order> dataList;
    private List<CourseListViewHolder> holderList = new ArrayList<>();

    public CourseListAdapter(Context context, List<OrderResult.Order> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @Override
    public CourseListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_order_course , parent , false);

        return new CourseListViewHolder(view);

    }

    @Override
    public void onBindViewHolder(CourseListViewHolder holder, final int position) {
        holder.setDataPosition(position);
        if (!(holderList.contains(holder))){
            holderList.add(holder);
        }

        final OrderResult.Order data = dataList.get(position);
        //老师图片暂定
//        ImageUtil.loadCircleImage(context , R.mipmap.iv_teacher_test , holder.lookingTeacherIv);

        CourseInfoView.CourseInfoData courseInfoData = new CourseInfoView.CourseInfoData();
        courseInfoData.setContent(data.getQuestionIntroduction());
        List<Member> memberList = new ArrayList<>();
        memberList.clear();
        memberList.addAll(data.getMemberList());
        courseInfoData.setMemberList(memberList);

        List<String> imgList = new ArrayList<>();
        if (!Validators.isEmpty(data.getQuestionImages())){
            String[] imgs = data.getQuestionImages().split(",");
            for (int i = 0 ; i < imgs.length ; i++){
                imgList.add(imgs[i]);
            }
        }
        courseInfoData.setImgList(imgList);
        holder.courseInfoView.bindData(courseInfoData);

        holder.orderedNumTv.setText("已接单(" + data.getApplyTeacherCount() + ")");

        holder.orderedLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                OrderedTeacherListActivity.startTeacherListActivity(context , data.getId() , data.getInitiatorId());
            }
        });

        holder.cancelTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                studentCancelOrder(data.getId() , position);
            }
        });

        holder.waitTimeTv.setText(data.getChangeTime());

    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    /**只刷新时间
     *
     */
    public void notifyData(){
        if (dataList.size() > holderList.size() || dataList.size() == holderList.size()){
            for (int i = 0 ; i < holderList.size() ; i ++){
                holderList.get(i).waitTimeTv.setText(dataList.get(holderList.get(i).position).getChangeTime());
            }
        }else{
            holderList.clear();
            notifyDataSetChanged();
        }
    }

    class CourseListViewHolder extends RecyclerView.ViewHolder{

        TextView waitTimeTv;
//        ImageView lookingTeacherIv;
        CourseInfoView courseInfoView;
        TextView orderedNumTv;
        RelativeLayout orderedLayout;
        TextView cancelTv;

        private int position;

        public void setDataPosition(int position){
            this.position = position;
        }

        public CourseListViewHolder(View itemView) {
            super(itemView);

            waitTimeTv = (TextView)itemView.findViewById(R.id.waitTimeTv);
//            lookingTeacherIv = (ImageView)itemView.findViewById(R.id.lookingTeacherIv);
            courseInfoView = (CourseInfoView)itemView.findViewById(R.id.courseInfoView);
            orderedNumTv =  (TextView)itemView.findViewById(R.id.orderedNumTv);
            orderedLayout = itemView.findViewById(R.id.orderedLayout);
            cancelTv = (TextView)itemView.findViewById(R.id.cancelTv);
        }
    }

    /**学生取消订单
     *
     * @param orderId
     */
    private void studentCancelOrder(String orderId , final int position){
        Map<String, String> map = new HashMap<>();
        map.put("studentId", SPUtil.stringOut(SPConstant.USER_ID));
        map.put("orderId", orderId);

        Observable observable = ApiFactory.getEducationApiSingleton().studentCancelOrder(SPUtil.stringOut(SPConstant.TOKEN), map);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber(context) {
            @Override
            protected void _onNext(Object o) {
                MyToast.show(context , "取消成功");
                dataList.remove(position);
                notifyDataSetChanged();
            }

            @Override
            protected void _onError(String message) {
                MyToast.show(context , message);
            }
        } , true);
    }
}
